package com.jeff.cloud.gatewayzuul.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: jeff
 * @date: 2021/8/12 10:04
 */
public class PreRequestLogFilter extends ZuulFilter {
    private static final Logger LOGGER = LoggerFactory.getLogger(PreRequestLogFilter.class);
    @Override
    public String filterType() {
        return "pre";//过滤器类型，有pre route post error
    }

    @Override
    public int filterOrder() {
        return 1;//指定过滤器执行顺序，不同的过滤器允许返回相同的数字
    }

    @Override
    public boolean shouldFilter() {
        return true;//判断该过滤器是否要执行，true 执行，false 不执行
    }

    @Override
    public Object run() {
        //过江器的具体逻辑，这里打印了请求方法及地址。
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        PreRequestLogFilter.LOGGER.info(String.format("send %s request to %s",request.getMethod(),request.getRequestURL().toString()));
        return null;
    }
}
